<template>
  <page-header-wrapper>
    <application-search @search="handleSearch" @reset="handleReset"> </application-search>
    <a-card :bordered="false">
      <s-table
        :scroll="{ x: 1800 }"
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <div slot="action" slot-scope="text, record">
          <y-button type="link" @click="handleDetail(record.ID)">查看</y-button>
          <y-button type="link" @click="handleEdit(record.ID)">修改</y-button>
          <y-button type="link" @click="handleAudit(record.ID)">审核</y-button>
        </div>
        <y-button slot="reback" slot-scope="text, record" type="link" class="text-error" @click="handleBack(record)"
          >退回</y-button
        >
      </s-table>
    </a-card>
    <a-modal
      title="提示"
      :visible="visibleBack"
      :confirm-loading="confirmLoading"
      @ok="handleBackConfirm"
      @cancel="handleBackCancel"
    >
      <a-form :form="backForm">
        <a-form-item label="退回原因">
          <a-textarea
            v-decorator="['ProcessInfo', { rules: [{ required: true, message: '请输入退回原因' }] }]"
            placeholder="请输入退回原因"
            :auto-size="{ minRows: 3, maxRows: 5 }"
          ></a-textarea>
        </a-form-item>
      </a-form>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis, buttonSelect, applicationSearch } from '@/components'
import { getProposerList, postProposerExamine } from '@/api/common'
import moment from 'moment'
export default {
  name: 'cityComplexInquireList',
  components: {
    STable,
    Ellipsis,
    buttonSelect,
    applicationSearch,
  },
  data() {
    this.$eventBus.$on('cityAuditRefresh', () => {
      this.$refs.table.refresh()
    })
    return {
      queryParam: {
        UserID: this.$store.getters.userid,
        Mode: 'BARecAudit',
        strContent: {},
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' },
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNo',
        },
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'Proposer',
        },
        {
          title: '身份证号',
          align: 'center',
          dataIndex: 'IDCard',
        },
        {
          title: '申请家庭类别',
          align: 'center',
          dataIndex: 'ApplyClass',
        },
        {
          title: '信息',
          align: 'center',
          customRender: (text, record) => {
            const days = moment().diff(moment(record.ProcessDate), 'days')
            if (days <= 16) {
              return <span class="text-success">离过期还剩{16 - days}天</span>
            } else {
              return <span class="text-error">已过期{days - 16}天</span>
            }
          },
        },
        {
          title: '区县',
          align: 'center',
          dataIndex: 'RegionName',
          customRender: (text) => {
            return <ellipsis length={16}>{text}</ellipsis>
          },
        },
        {
          title: '街道办事处',
          align: 'center',
          dataIndex: 'TownshipName',
          customRender: (text) => {
            return <ellipsis length={16}>{text}</ellipsis>
          },
        },
        {
          title: '申请保障人数',
          align: 'center',
          dataIndex: 'CityVfyNumber',
        },

        {
          title: '接收日期',
          align: 'center',
          dataIndex: 'ProcessDate',
          customRender: (text) => moment(text).format('YYYY-MM-DD HH:mm:ss'),
        },
        {
          title: '操作',
          align: 'center',
          width: '180px',
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
        {
          title: '退回',
          align: 'center',
          width: 60,
          fixed: 'right',
          scopedSlots: { customRender: 'reback' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getProposerList(requestParameters).then((res) => {
          return res
        })
      },
      backForm: this.$form.createForm(this, { name: 'back' }),
      visibleBack: false,
      confirmLoading: false,
      editData: {},
    }
  },
  methods: {
    handleSearch(values) {
      this.queryParam.strContent = values
      this.$refs.table.refresh(true)
    },
    handleReset() {
      this.queryParam.strContent = {}
      this.$refs.table.refresh(true)
    },
    handleDetail(id) {
      localStorage.setItem('browserCacheDetail', JSON.stringify({ id: id }))
      this.$router.push({
        name: 'filingDataReceiveReviewDetail',
        params: {
          id,
        },
      })
    },
    handleEdit(id) {
      localStorage.setItem('browserCacheEdit', JSON.stringify({ id: id }))
      this.$router.push({
        name: 'filingDataReceiveReviewEdit',
        params: {
          id,
        },
      })
    },
    handleAudit(id) {
      this.$router.push({
        name: 'filingDataReceiveReviewAudit',
        params: {
          id,
          type: 'city',
        },
      })
    },

    handleBack(record) {
      this.visibleBack = true
      this.editData = record
      this.$nextTick(() => {
        this.backForm.resetFields()
      })
    },
    handleBackConfirm() {
      this.backForm.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          postProposerExamine({
            State: 'CityExitAudit',
            ProposerModules: {
              ID: this.editData.ID,
              ProcessInfo: values.ProcessInfo,
              ProcessDate: moment().format('YYYY-MM-DD HH:mm:ss'),
              LastAlterDate: moment().format('YYYY-MM-DD HH:mm:ss'),
              LastAlterUserID: this.$store.getters.userid,
              ProcessUser: this.$store.getters.nickname,
            },
          })
            .then((res) => {
              this.editData = {}
              this.visibleBack = false
              this.$message.success('退回成功')
              this.$refs.table.refresh()
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleBackCancel() {
      this.backForm.resetFields()
      this.editData = {}
      this.visibleBack = false
    },
  },
}
</script>
<style lang="less" scoped></style>
